<template>
  <el-dialog title="上传施工图设计" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
    <el-form ref="form" :model="form" label-width="100px">
      <el-row>
        <el-col :span="12"> <el-form-item label="标段">
        <el-select v-model="form.titleNo" placeholder="请选择标段">
          <el-option value="第三" label="第三" />
          <el-option value="第四" label="第四" />
          <el-option value="第五" label="第五" />
        </el-select>
      </el-form-item></el-col>
        <el-col :span="12"><el-form-item label="名称">
        <el-input v-model="form.title"></el-input>
      </el-form-item></el-col>
      </el-row>
      <el-row>
        <el-col :span="24"><el-form-item label="备注">
        <el-input v-model="form.changeInfo"></el-input>
      </el-form-item></el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="图纸">
        <el-upload
          name="file"
          class="upload-demo"
          action="string"
          :http-request="beforeAvatarUploadThree"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="3"
          :on-success="handlesuccess"
          :on-error="handleerror"
          :on-progress="uploadVideoProcess"
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
        
<el-progress v-if="progressFlag" :percentage="loadProgress"></el-progress>
      </el-form-item>
        </el-col>
      </el-row>
    
     
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="create">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import api from '../../../../api'
import qs from 'qs'

export default {
  data() {
    return {
      dialogVisible: false,
      tunnelList: [],
      loadProgress: 0, // 动态显示进度条
      progressFlag: false, // 关闭进度条
      form: {
        tId: '', //隧道id
        titleNo: '', //图号
        title: '', // 图名
        resources: '', // 资源地址
        changeInfo:'',
 
      },
      fileList: []
    }
  },
  mounted() {
    this.$bus.$on('onAddAddrPlaneShow', (flag) => {
      this.dialogVisible = flag
    })
    // this.findByTunnelInfo()
  },
  methods: {
    // 查询隧道
    findByTunnelInfo() {
      api
        .tunnelInfoFindByAll()
        .then((res) => {
          if (res.data.code == 2001) {
            this.tunnelList = res.data.data
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    create() {
      api
        .AddrPlaneFileSave(qs.stringify(this.form))
        .then((res) => {
          if (res.data.code == 2001) {
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.dialogVisible = false
            this.$bus.$emit('onRefreshAddrPlane', true)
          }
        })
        .catch((err) => {})
    },
    // 上传图片前的过滤
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1000
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 200MB!')
      }
      return isLt2M
    },
    handlePreview(file) {
      console.log(file)
    },
    handlesuccess(response, file, fileList) {
      console.log(response)
      if (response.code == 2001) {
        this.form.resources = response.data
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleerror(err, file, fileList) {
      console.log(err)
    },
    beforeAvatarUploadThree(file) {
      let params = new FormData()
      params.append('file', file.file)
      api
        .AddrPlaneFileUpload(params)
        .then((res) => {
          if (res.data.code == 2001) {
            this.form.resources = res.data.data
          }else{
            this.$message({
              type: 'fail',
              message: res.data.message
            })
          }
        })
    },
    uploadVideoProcess(event, file, fileList){
      this.progressFlag = true; // 显示进度条
          this.loadProgress = parseInt(event.percent); // 动态获取文件上传进度
          if (this.loadProgress >= 100) {
              this.loadProgress = 100
              setTimeout( () => {this.progressFlag = false}, 1000) // 一秒后关闭进度条
          }
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  width: 50px !important;
  height: 50px !important;
  font-size: 28px;
  line-height: 50px !important;
  color: #8c939d;
  text-align: center;
}
.avatar {
  display: block;
  width: 50px !important;
  height: 50px !important;
}
.imgbeforepad {
  padding-top: 0px !important;
}
.avatar-uploader .el-icon-plus {
  margin: 15px !important;
}
</style>